package com.example.jacky.android_easeui.demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.jacky.android_easeui.R;
import com.example.jacky.android_easeui.base.BaseActivity;
import com.example.jacky.android_easeui.demo.bitmap.ColorChangeActivity;
import com.example.jacky.android_easeui.demo.bitmap.CopyBitmapActivity;
import com.example.jacky.android_easeui.demo.bitmap.ForgirlActivity;
import com.example.jacky.android_easeui.demo.bitmap.MirrorActivity;
import com.example.jacky.android_easeui.demo.bitmap.RotationActivity;
import com.example.jacky.android_easeui.demo.bitmap.RoundRectBitmapActivity;
import com.example.jacky.android_easeui.utils.LogUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class BitmapActivity extends BaseActivity {
    public static final String TAG = "BitmapActivity";
    @Bind(R.id.iv)
    ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);
        ButterKnife.bind(this);
    }

    /**
     * 加载图片到内存
     *
     * @param view
     */
    public void click(View view) {
        //相当消耗内存资源 根据图片的分辨率而定
        // Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg");
        // iv.setImageBitmap(bitmap);

        //1.得到屏幕的宽高信息
        WindowManager wm = getWindowManager();
        int screenWidth = wm.getDefaultDisplay().getWidth();
        int screenHeight = wm.getDefaultDisplay().getHeight();
        LogUtil.info("屏幕宽高：" + screenWidth + "-" + screenHeight);

        //2.得到图片的宽高。
        BitmapFactory.Options opts = new BitmapFactory.Options();//解析位图的附加条件
        opts.inJustDecodeBounds = true;//不去解析真实的位图，只是获取这个位图的头文件信息
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.negative_img, opts);
        int bitmapWidth = opts.outWidth;
        int bitmapHeight = opts.outHeight;
        LogUtil.info("图片宽高： " + bitmapWidth + "-" + bitmapHeight);

        //3.计算缩放比例
        int dx = bitmapWidth / screenWidth;
        int dy = bitmapHeight / screenHeight;
        LogUtil.info("dx=" + dx + " ,dy=" + dy);
        int scale = 1;
        if (dx > dy && dy > 1) {
            LogUtil.info("按照水平方法缩放,缩放比例：" + dx);
            scale = dx;
        }

        if (dy > dx && dx > 1) {
            LogUtil.info("按照垂直方法缩放,缩放比例：" + dy);
            scale = dy;
        }
        //4.缩放加载图片到内存。
        opts.inSampleSize = scale;
        opts.inJustDecodeBounds = false;//真正的去解析这个位图。
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.negative_img, opts);
        mIv.setImageBitmap(bitmap);
    }

    public void turn2Next(View view) {
        switch (view.getId()) {
            case R.id.button1:
                forward(MirrorActivity.class);
                break;
            case R.id.button2:
                forward(CopyBitmapActivity.class);
                break;
            case R.id.button3:
                forward(RotationActivity.class);
                break;
            case R.id.button4:
                forward(ColorChangeActivity.class);
                break;
            case R.id.button5:
                forward(ForgirlActivity.class);
                break;
            case R.id.button6:
                forward(RoundRectBitmapActivity.class);
                break;
        }
    }
}
